package com.pacy.pacybaseproject.app;

import android.app.Activity;
import android.util.Log;


import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.common.BaiduMapSDKException;
import com.blankj.utilcode.util.ActivityUtils;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.pacy.pacybaseproject.MainActivity;
import com.pacy.pacybaseproject.utils.ConfigUtils;

import io.reactivex.annotations.NonNull;
import me.goldze.mvvmhabit.base.BaseApplication;
import me.goldze.mvvmhabit.crash.CaocConfig;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.Utils;

/**
 * @author PACY
 * @date 2023/8/16
 * description:
 */
public class XApp extends BaseApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        initCrash();
        Utils.init(this);
        initLog();
        initBaiduMap();
        ActivityUtils.addActivityLifecycleCallbacks(new com.blankj.utilcode.util.Utils.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity) {
                super.onActivityCreated(activity);
                Log.e("WJApp", "onActivityCreated: " + activity.getClass().getSimpleName());
            }
        });

//        JPushInterface.setDebugMode(true);
//        JPushInterface.init(this);
    }

    /**
     * 初始化日志
     */
    private void initLog() {
        KLog.init(ConfigUtils.isDebug);
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(true)                   //是否打印当前线程
                .methodCount(0)                         //方法数显示多少行，默认两行
                .methodOffset(5)                        //隐藏方法内部调用到偏移量，默认5
                .tag(ConfigUtils.TAG)                     //自定义TAG，Logger打印会使用同一个TAG
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
    }

    /**
     * 初始化异常捕获
     */
    private void initCrash() {
        CaocConfig.Builder.create()
                .enabled(true) //是否启动全局异常捕获
                .showErrorDetails(true) //是否显示错误详细信息
                .showRestartButton(true) //是否显示重启按钮
                .trackActivities(true) //是否跟踪Activity
                .minTimeBetweenCrashesMs(2000) //崩溃的间隔时间(毫秒)
                .restartActivity(MainActivity.class) //重新启动后的activity
//                .errorActivity(CustomClashActivity.class) //崩溃后的错误activity
//                .eventListener(new CustomActivityOnCrash.EventListener() {
//                    @Override
//                    public void onLaunchErrorActivity() {
//
//                    }
//
//                    @Override
//                    public void onRestartAppFromErrorActivity() {
//
//                    }
//
//                    @Override
//                    public void onCloseAppFromErrorActivity() {
//
//                    }
//                }) //崩溃后的错误监听
                .apply();
    }

    private void initBaiduMap(){
        // 是否同意隐私政策，默认为false
        SDKInitializer.setAgreePrivacy(this, true);
        try {
            // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
            SDKInitializer.initialize(this);
            SDKInitializer.setCoordType(CoordType.BD09LL);
        } catch (BaiduMapSDKException e) {

        }

    }
}
